package cn.texous.demo.dos.config.mongo;

import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * insert description here
 *
 * @param <T> entity
 * @author leven
 * @since 2019/7/17 18:38
 */
public class MongoBaseDaoAdapter<T extends MongoBaseEntity> implements MongoBaseDao<T> {

    protected MongoTemplate mongoTemplate;
    protected Class<T> entityClass = (Class<T>)
            ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    protected String tabName = entityClass.getSimpleName();

    public MongoBaseDaoAdapter(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    @Override
    public T findOne(Query query) {
        return mongoTemplate.findOne(query, entityClass, tabName);
    }

    @Override
    public List<T> find(Query query) {
        return mongoTemplate.find(query, entityClass, tabName);
    }

    @Override
    public MongoPageList<T> find(Query query, Pageable pageable) {
        MongoPageList<T> mongoPageList = new MongoPageList<>();
        if (pageable != null) {
            long totalCount = count(query);
            int pageCount = (int) (totalCount / pageable.getPageSize());
            if (totalCount % pageable.getPageSize() != 0) {
                pageCount += 1;
            }
            query.with(pageable);
            mongoPageList.makePageList(null, pageable.getPageSize(),
                    totalCount, pageable.getPageNumber(), pageCount);
        }
        mongoPageList.setPage(mongoTemplate.find(query, entityClass, tabName));
        return mongoPageList;
    }

    /**
     * 通过反射将对象的值设置到update中
     *
     * @param obj obj
     * @param curClass curClass
     * @param update update
     */
    private void setClassFieldToUpdate(Object obj, Class curClass, Update update) {
        Field[] obj_fields = curClass.getDeclaredFields();
        try {
            for (Field field : obj_fields) {
                if (Modifier.isFinal(field.getModifiers())
                        || Modifier.isPublic(field.getModifiers())
                        || Modifier.isStatic(field.getModifiers())) {
                    continue;
                }
                field.setAccessible(true);
                boolean isFixed = false;
                Annotation[] annotations = field.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation.getClass() == MongoFixed.class) {
                        isFixed = true;
                    }
                }
                //如果字段是不变的则不添加在update中
                if (isFixed) {
                    continue;
                }
                update.set(field.getName(), field.get(obj));
            }
            if (curClass.getSuperclass() != null) {
                setClassFieldToUpdate(obj, curClass.getSuperclass(), update);//递归获取父类的字段值
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public long updateAll(T t) {
        Update update = new Update();
        setClassFieldToUpdate(t, entityClass, update);
        return mongoTemplate.updateFirst(Query.query(Criteria.where(MongoBaseEntity._ID)
                .is(t.get_id())), update, entityClass, tabName).getModifiedCount();
    }

    @Override
    public long update(Query query, Update update) {
        return mongoTemplate.updateFirst(query, update, entityClass, tabName).getModifiedCount();
    }

    @Override
    public long updateMulti(Query query, Update update) {
        return mongoTemplate.updateMulti(query, update, entityClass, tabName).getModifiedCount();
    }

    @Override
    public T findAndModify(Query query, Update update, boolean isNew) {
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(isNew);
        findAndModifyOptions.upsert(true);
        return mongoTemplate.findAndModify(query, update,
                findAndModifyOptions, entityClass, tabName);
    }

    @Override
    public void insert(T t) {
        mongoTemplate.insert(t, tabName);
    }

    @Override
    public void insert(List<T> tList) {
        mongoTemplate.insertAll(tList);
    }

    @Override
    public long delete(Query query) {
        return mongoTemplate.remove(query, entityClass, tabName).getDeletedCount();
    }

    @Override
    public long count(Query query) {
        return mongoTemplate.count(query, entityClass, tabName);
    }

    @Override
    public boolean isExist(Query query) {
        return mongoTemplate.exists(query, entityClass, tabName);
    }

}
